﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CRMEntity;
using CRMBiz;

namespace CRMConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalContactAmount = 0;
            int index = 1;
            List<FollowUpContact> contactList = null;
            try
            {
                ContactFollowUpHandler.Setup();
                contactList = ContactFollowUpHandler.GetFollowUpList();
                totalContactAmount = contactList.Count();
            }
            catch (Exception ex)
            {
                WriteLogFile(ex.Message);
            }
           
          
            if (totalContactAmount > 0)
            {
                var followUpList = from c in contactList where !c.IsEmailNull select c;
                
                if (followUpList.Count() > 0)
                {
                    Console.WriteLine("Sending following up email is starting...");
                 
                    foreach (FollowUpContact c in followUpList)
                    {
                        try
                        {
                            if (IsValidEmailAddress(c.EmailAddress))
                            {
                                Console.WriteLine("Sending follow up Email and SMS {0} of {1}...", index, followUpList.Count());
                                ContactFollowUpHandler.SendEmail(c);

                                if (!string.IsNullOrEmpty(c.MobileNumber))
                                {
                                    ContactFollowUpHandler.SendMessage(c);
                                }
                            }
                            else
                            {
                                WriteLogFile("***Error*** Contact ID: " + c.ContactId.ToString() + " has invalid email address " + c.EmailAddress);
                            }
                           
                            index += 1;
                          //  WriteLogFile("************success");
                        }
                        catch (Exception ex)
                        {
                            WriteLogFile("Application no: "+c.ApplicationId.ToString()+" "+ ex.Message);
                        }
                    }
                    Console.WriteLine("Sending following up Email and SMS is finished.");
                }
            }
        }
     
        /// <summary>
        /// Write error message to log file while error happened
        /// </summary>
        /// <param name="followUpInfo"></param>
        /// <param name="errorMessage"></param>
        private static void WriteLogFile(string errorMessage)
        {
            using (var writer = new StreamWriter(new FileStream("SendFollowUpErrorLog.txt", FileMode.Append)))
            {
                writer.WriteLine("Time: {0}  Error: {1}", DateTime.Now.ToString(), errorMessage);
            }
        }
        /// <summary>
        /// Check client email address is valid
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <returns></returns>
        private static bool IsValidEmailAddress(string emailAddress)
        {
            try
            {
                var validEmailAddress = new System.Net.Mail.MailAddress(emailAddress);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
